from typing import List

from chatchat.server.pydantic_v1 import Field

from .tools_registry import BaseToolOutput, regist_tool


@regist_tool(title="数学计算器")
def calculate(expression: str = Field("1+1",description="a math expression")) -> List[float]:
    """
    Useful for simple calculations.
    Translate the user's question into a numexpr-evaluable expression that contains only digits and operators (no variables or words).
    """
    import numexpr

    try:
        exps = expression.split(",")
        ret = list()
        for exp in exps:
            ret.append(str(numexpr.evaluate(exp)))
        print(ret)
    except Exception as e:
        ret = f"wrong: {e}"

    return BaseToolOutput(ret)
